<template>
  <div>
    <div>
      <el-dialog title="编辑角色" :visible.sync="$props.visible" width="50%" @close="handleClose">

        <div class="add-content">
          <el-form
            ref="formName"
            :model="staffFrom"
            :rules="staffRules"
            label-width="120px"
            style="margin-top: 10px;"
          >
            <el-form-item label="角色名称：" prop="name">
              <el-input v-model="staffFrom.name" placeholder="请输入角色名称" style="width: 60%;" />
            </el-form-item>
            <el-form-item label="角色描述：" prop="descpt">
              <el-input v-model="staffFrom.descpt" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="请输入角色描述" style="width: 60%;" />
            </el-form-item>
          </el-form>
        </div>

        <span class="dialog-footer">
          <div style="margin-top:15px;">
            <el-button @click="handleClose">取 消</el-button>
            <el-button type="primary" @click="submitClick">提交</el-button>
          </div>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { roleEdit } from '@/api/iam/iamRole'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    rowData: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      staffFrom: {
        name: '',
        descpt: ''
      },
      staffRules: {
        name: [{ required: true, message: '请填写角色名称', trigger: 'blur' }],
        descpt: [{ required: true, message: '请填写角色描述', trigger: 'blur' }]
      },
      usages: '',
      id: ''
    }
  },
  created() {
    console.log('角色详情', this.rowData)
    this.staffFrom.name = this.rowData.name
    this.staffFrom.descpt = this.rowData.descpt
    this.id = this.rowData.id
  },
  methods: {
    // 弹出框关闭后触发
    handleClose() {
      // 子组件调用父组件方法，并传递参数
      this.$emit('handleClose')
    },
    handleSuccess() {
      this.handleClose()
      this.$emit('handleSuccess')
    },
    submitClick() {
      this.$refs['formName'].validate((valid) => {
        if (valid) {
          const data = {
            'name': this.staffFrom.name,
            'descpt': this.staffFrom.descpt,
            'usages': this.usages
          }
          roleEdit(data, this.id).then((response) => {
            this.$message({
              message: '编辑成功',
              type: 'success'
            })
            this.handleSuccess()
          })
        }
      }
      )
    }
  }
}
</script>
<style>
.add-content {
    border: 1px solid #e4e4e4;
    width: 90%;
    margin: 0 auto;
    height: 250px;
}
.dialog-footer{
    width: 97%;
    display: flex;
    justify-content:center;
}
</style>
